package com.cm.etax.sms.bean;

import com.cm.etax.sms.exceptions.SmsException;

import java.util.Objects;

/**
 * 短信服务响应码
 *
 * @author LWang 2023.02.01
 * @since 1.0.0
 */
public record SmsResult(
        int code,
        String message,
        SmsCode data
) {
    /**
     * 响应码：成功 {@value }
     */
    public final static int SUCCESS = 200;

    /**
     * 响应码：执行中 {@value }
     */
    public final static int PROCESSING = 201;

    /**
     * 响应码：任务重复 {@value }
     */
    public final static int REPEAT = 401;

    /**
     * 响应码：超过每日限额 {@value }
     */
    public final static int LIMIT = 402;

    /**
     * 响应码：任务不存在 {@value }
     */
    public final static int NONEXISTENT = 404;

    /**
     * 响应码未知异常 {@value }
     */
    public final static int UNKNOWN = 499;

    public static SmsResult handle(SmsResult result) {
        if (Objects.isNull(result)) {
            throw new IllegalArgumentException("缺少参数：result");
        }
        switch (result.code) {
            case REPEAT, NONEXISTENT -> throw new SmsException(SmsException.PHONE_LOCK, result.message);
            case LIMIT -> throw new SmsException(SmsException.PHONE_LIMIT_RANGE, result.message);
            case UNKNOWN -> throw new SmsException(SmsException.NET_ERROR, result.message);
        }
        return result;
    }
}
